﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Facebook.Rest;
using Facebook.Utility;
using CityLife.Objects;
using Facebook.Schema;

namespace CityLife.Controls
{
    public partial class AddNeighbours : FloatableWindow
    {

        public user SelectedUser;


        public AddNeighbours()
        {
            InitializeComponent();


            

            MainPage._facebookAPI.Friends.GetAppUsersObjectsAsync(new Users.GetInfoCallback(GetAppUserObjectsCompleted), null);
            MainPage._facebookAPI.Friends.GetUserObjectsAsync(new Users.GetInfoCallback(GetUserObjectsCompleted), null);

            PlayersListLoader.IsBusy = true;
            FriendsListLoader.IsBusy = true;

            FriendsList.SelectionChanged+=new SelectionChangedEventHandler(FriendsList_SelectionChanged);
            PlayersList.SelectionChanged+=new SelectionChangedEventHandler(PlayersList_SelectionChanged);
        }


        private void GetUserObjectsCompleted(IList<user> users, object state, FacebookException e)
        {

            Dispatcher.BeginInvoke(delegate()
            {
                List<user> cleanlist = new List<user>();
                foreach (user u in users)
                {
                    if (u.uid == MainPage.Heart.City.UserID)
                        continue;

                    if (!MainPage.Heart.City.Neighboures.ContainsKey(Convert.ToInt64(u.uid)))
                        cleanlist.Add(u);

                }
                FriendsList.ItemsSource = cleanlist;
                FriendsListLoader.IsBusy = false;
            });

        }

        private void GetAppUserObjectsCompleted(IList<user> users, object state, FacebookException e)
        {

            Dispatcher.BeginInvoke(delegate()
            {
                List<user> cleanlist = new List<user>();
                foreach (user u in users)
                {
                    if (u.uid == MainPage.Heart.City.UserID)
                        continue;

                    if (!MainPage.Heart.City.Neighboures.ContainsKey(Convert.ToInt64(u.uid)))
                        cleanlist.Add(u);

                }
                PlayersList.ItemsSource = cleanlist;
                PlayersListLoader.IsBusy = false;
            });

        }


        

  

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {

            this.DialogResult = true;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void FriendsList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            user u = e.AddedItems[0] as user;
            SelectedUser = u;

        }

        private void PlayersList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
          user u = e.AddedItems[0] as user;
          SelectedUser = u;
        }
    }
}

